uses Windows;

var CtrlClassNum:integer;
const CtrlClassName = 'MinCL_Ctrl_Class_';
{&stdcall+}
function AllWndProc(H:DWord;msg:DWord;wprm:LongInt;lprm:LongInt):longint;{$ifndef virtualpascal}stdcall;{$endif}
var
  p: Pointer;
  w: TWinControl;
  m:TMessage;
  i: Integer;
begin
  //Write('Processing message '+IntToHex(msg,4)+' for window ',h,'...');
  for i:=0 to AllWnds.Count-1 do
  begin
    p:=AllWnds.Items[i];
    if not assigned(P) then Continue;
    if not (TObject(p) is TWinControl) then Continue;

    w:=TWinControl(p);
    if w.Handle<>H then Continue;
    M.msg:=msg;
    m.wParam:=wprm;
    m.lParam:=lprm;
    m.Result:=1;
    w.Dispatch(m);
    Result:=m.Result;
    if Result<>1 then exit;
  end;
  Result:=DefWindowProc(h,msg,wprm,lprm);
  //Writeln('done');
end;
{&stdcall-}

{ TWinControl }

function TWinControl.GetRealWindow: boolean;
begin
  Result:=true;//Anyway
end;

procedure TWinControl.SetLeft(AValue: integer);

begin
  inherited SetLeft(AValue);
  if not RealWindow then exit;
  if FHandle<>0 then
   MoveWindow(FHandle,FLeft,FTop,FWidth,FHeight,true);
end;

procedure TWinControl.SetTop(AValue: integer);
begin
  inherited SetTop(AValue);
  if not RealWindow then exit;
  if FHandle<>0 then
   MoveWindow(FHandle,FLeft,FTop,FWidth,FHeight,true);
end;

procedure TWinControl.SetWidth(AValue: integer);
begin
  inherited SetWidth(AValue);
  if not RealWindow then exit;

  if FHandle<>0 then
   MoveWindow(FHandle,FLeft,FTop,FWidth,FHeight,true);
end;

procedure TWinControl.SetHeight(AValue: integer);
begin
  inherited SetHeight(AValue);
  if not RealWindow then exit;
  if FHandle<>0 then
    MoveWindow(FHandle,FLeft,FTop,FWidth,FHeight,true);
end;




procedure TWinControl.SetParent(AValue: TComponent);
var
  ParHan: THandle;
  Messg: mSets.TMessage;
  WCls:TWNDCLASS;
begin
  inherited SetParent(AValue);
  if Parent=nil then exit;
  if not (Parent is TWinControl) then exit;
  ParHan:=(Parent as TWinControl).Handle;
  if ParHan=0 then exit;
  if FSpecificHandling then exit;//Not here
  if FWinCreated then //TODO: Reparent
  else
  begin
    inc(CtrlClassNum);
    FCtrlExtData:=CtrlClassName+IntToStr(CtrlClassNum+1);
    WCls.style:=CS_OWNDC;
    WCls.lpfnWndProc:=@AllWndProc;
    Wcls.cbClsExtra:=0;
    WCls.cbWndExtra:=0;
    WCls.hInstance:=HINSTANCE;
    WCls.hIcon:=0;
    wcls.hCursor:=0;
    wcls.hbrBackground:=0;
    WCls.lpszMenuName:=nil;
    WCls.lpszClassName:=@FCtrlExtData[1];
    writeln(RegisterClass(WCls),' ',GetLastError);
    FHandle:=CreateWindow(@FCtrlExtData[1],@Caption[1],WS_CHILD  or WS_VISIBLE,FLeft,FTop,FWidth,FHeight,
      (AValue as TWinControl).Handle,0,HINSTANCE,nil);
    if FHandle=0 then Writeln('Error:',GetLastError);
    Messg.MSG:=WM_PAINT;
    Dispatch(Messg);
  end;
end;

procedure TWinControl.Show;
begin
  inherited Show;
  if not RealWindow then exit;
  if FHandle=0 then exit;

  ShowWindow(FHandle,SW_NORMAL);
  if Handle<>0 then if FCanvas.Handle<>0 then FCanvas.ActualizeWin(FHandle);
end;

destructor TWinControl.Destroy;
var
  i: Integer;
begin
  inherited Destroy;
  FCanvas.Free;

  if FCtrlExtData<>'' then UnregisterClass(@FCtrlExtData[1],HINSTANCE);
  i:=AllWnds.IndexOf(self);
  if i<>-1 then AllWnds.Delete(i);
end;

procedure TControl.ProcessEvent(Ev: pointer);
begin
  //May be somewhat usefull under win, but not now
end;

var CLS:TWNDCLASS;
procedure InitMCTrl;
begin
CtrlClassNum:=0;
while LongInt(GetClassInfo(HINSTANCE,PChar(CtrlClassName+IntToStr(CtrlClassNum)),cls))<>0 do
  Inc(CtrlClassNum);
end;
